package online.yangcloud.web.service.meta.impl;

import cn.hutool.core.util.ObjUtil;
import com.mybatisflex.core.query.QueryWrapper;
import online.yangcloud.common.annotation.TimeConsuming;
import online.yangcloud.common.common.AppConstants;
import online.yangcloud.common.enumration.YesOrNoEnum;
import online.yangcloud.common.model.business.list.ListSplitBusiness;
import online.yangcloud.common.tools.ListTools;
import online.yangcloud.core.mapper.GlobalMapper;
import online.yangcloud.core.model.entity.FileBlockEntity;
import online.yangcloud.web.service.meta.FileBlockService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhuboyang
 * @since 2023年06月10 22:19:43
 */
@Service
@Transactional(rollbackFor = Exception.class)
@TimeConsuming
public class FileBlockServiceImpl implements FileBlockService {

    @Resource
    private GlobalMapper globalMapper;

    @Override
    public void batchInsert(List<FileBlockEntity> fileBlocks) {
        while (ObjUtil.isNotNull(fileBlocks) && !fileBlocks.isEmpty()) {
            ListSplitBusiness<FileBlockEntity> business = ListTools.split(fileBlocks, AppConstants.Batch.COUNT);
            globalMapper.acquireFileBlock().insertBatch(business.getSplits());
            fileBlocks = business.getSources();
        }
    }

    @Override
    public List<FileBlockEntity> queryBlocks(String fileId) {
        return globalMapper.acquireFileBlock().selectListByQuery(QueryWrapper.create()
                .where(FileBlockEntity::getFileId).eq(fileId)
                .and(FileBlockEntity::getIsDelete).eq(YesOrNoEnum.NO.code())
                .orderBy(FileBlockEntity::getCreateTime).asc());
    }

}
